using Avalonia.BiliBili.Units;
using Avalonia.Controls;
using Avalonia.Controls.Templates;
using Avalonia.Data;
using Avalonia.Extensions.Controls;
using Avalonia.Extensions.Event;
using Avalonia.Input;
using Bilibili.Common;
using Bilibili.Common.Log;
using Bilibili.Model.Live;
using Bilibili.WebClient.Api;
using PCLUntils.Json;
using System;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Threading.Tasks;

namespace Avalonia.BiliBili.Windows
{
    public partial class AllLiveAreaWindow : WindowBase
    {
        private LiveHomeClient Client { get; }
        private ObservableCollection<TabItem> Items { get; }
        public AllLiveAreaWindow()
        {
            InitializeComponent();
            Client = new LiveHomeClient();
            Items = new ObservableCollection<TabItem>();
        }
        protected override void AfterInitializeComponent()
        {
            tabControl.ItemsSource = Items;
            RunOnUiThread(LoadData);
        }
        private async Task LoadData()
        {
            try
            {
                var model = await Client.GetAreaList();
                if (model.IsSuccess())
                {
                    var template = new FuncDataTemplate<AreaListItem>((_, __) =>
                    {
                        StackPanel stackPanel = new StackPanel
                        {
                            Orientation = Layout.Orientation.Vertical
                        };
                        ImageBox image = new ImageBox
                        {
                            Width = 64,
                            HorizontalAlignment = Layout.HorizontalAlignment.Center,
                            [!ImageBox.SourceProperty] = new Binding("pic")
                        };
                        stackPanel.Children.Add(image);
                        TextBlock textBlock = new TextBlock
                        {
                            [!TextBlock.TextProperty] = new Binding("name"),
                            HorizontalAlignment = Layout.HorizontalAlignment.Center
                        };
                        stackPanel.Children.Add(textBlock);
                        return stackPanel;
                    });
                    foreach (var item in model.Data)
                    {
                        var listBox = new GridView();
                        TabItem tabItem = new TabItem
                        {
                            Content = listBox,
                            Header = item.name
                        };
                        listBox.ItemsSource = item.list;
                        listBox.ColumnNum = 5;
                        listBox.ItemTemplate = template;
                        listBox.ItemClick += ListBoxOnItemClick;
                        Items.Add(tabItem);
                    }
                }
            }
            catch (Exception ex)
            {
                LogManager.Instance.LogError("LoadData", ex);
            }
        }
        private void ListBoxOnItemClick(object sender, ViewRoutedEventArgs e)
        {
            try
            {
                if (e.ClickMouse == MouseButton.Left && e.ClickItem is GridViewItem item && item.Content is AreaListItem model)
                {
                    var area_id = model.id.ToString();
                    var parent_area_id = model.parent_id.ToString();
                    var data = (new { model.name, area_id, parent_area_id }).ParseJson();
                    Navigater.OpenWindow<LiveAreaWindow>(data);
                }
            }
            catch { }
        }
    }
}